package cn.heyige.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.heyige.backend.common.CommResponse;
import cn.heyige.backend.dto.MemberDto;
import cn.heyige.backend.entity.MemberLevelDO;
import cn.heyige.backend.facade.MemberFacade;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/members")
@RequiredArgsConstructor
@Tag(name = "会员管理", description = "会员相关接口")
public class MemberController {

    private final MemberFacade memberFacade;

    /**
     * 获取所有会员等级
     */
    @GetMapping("levels")
    @Operation(summary = "获取所有会员等级", description = "获取系统中的所有会员等级列表")
    public CommResponse<List<MemberLevelDO>> getAllMemberLevels() {
        Page<MemberLevelDO> page = memberFacade.getAllMemberLevels(0, 100);
        return CommResponse.ok(page.getRecords());
    }

    /**
     * 获取会员等级详情
     */
    @GetMapping("levels/{id}")
    @Operation(summary = "获取会员等级详情", description = "根据ID获取会员等级详情")
    public CommResponse<MemberLevelDO> getMemberLevel(
            @Parameter(description = "会员等级ID", required = true, example = "1") @PathVariable Long id) {
        MemberLevelDO level = memberFacade.getMemberLevel(id);
        return CommResponse.ok(level);
    }

    /**
     * 创建会员订单
     */
    @PostMapping("orders")
    @Operation(summary = "创建会员订单", description = "创建购买会员的订单")
    public CommResponse<MemberDto.OrderResponse> createMemberOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Integer userId,
            @Parameter(description = "创建订单请求参数") @Valid @RequestBody MemberDto.CreateOrderRequest request) {
        MemberDto.OrderResponse response = memberFacade.createMemberOrder(userId, request);
        return CommResponse.ok(response);
    }

    /**
     * 支付会员订单
     */
    @PostMapping("orders/{orderId}/pay")
    @Operation(summary = "支付会员订单", description = "支付指定的会员订单")
    public CommResponse<MemberDto.OrderResponse> payMemberOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "订单ID", required = true, example = "1") @PathVariable Long orderId,
            @Parameter(description = "支付请求参数") @Valid @RequestBody MemberDto.PayRequest request) {
        MemberDto.OrderResponse response = memberFacade.payMemberOrder(orderId, userId, request);
        return CommResponse.ok(response);
    }

    /**
     * 取消会员订单
     */
    @PostMapping("orders/{orderId}/cancel")
    @Operation(summary = "取消会员订单", description = "取消指定的会员订单")
    public CommResponse<Void> cancelMemberOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "订单ID", required = true, example = "1") @PathVariable Long orderId) {
        memberFacade.cancelMemberOrder(orderId, userId);
        return CommResponse.ok();
    }

    /**
     * 获取用户的会员订单列表
     */
    @GetMapping("orders/my")
    @Operation(summary = "获取用户会员订单", description = "分页获取用户的会员订单列表")
    public CommResponse<Page<MemberDto.OrderResponse>> getUserMemberOrders(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {

        Page<MemberDto.OrderResponse> orders = memberFacade.getUserMemberOrders(userId, page, size);
        return CommResponse.ok(orders);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("orders/{orderId}")
    @Operation(summary = "获取订单详情", description = "根据订单ID获取订单详情")
    public CommResponse<MemberDto.OrderResponse> getMemberOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "订单ID", required = true, example = "1") @PathVariable Long orderId) {
        MemberDto.OrderResponse response = memberFacade.getMemberOrder(orderId, userId);
        return CommResponse.ok(response);
    }

    /**
     * 检查用户VIP状态
     */
    @GetMapping("vip/status")
    @Operation(summary = "检查VIP状态", description = "检查用户的VIP状态")
    public CommResponse<MemberDto.VipStatusResponse> checkVipStatus(
            @Parameter(hidden = true) @RequestAttribute("userId") Integer userId) {
        MemberDto.VipStatusResponse response = memberFacade.checkVipStatus(userId);
        return CommResponse.ok(response);
    }
}